package com.example.appsnight;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import shared.Constants;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.NavUtils;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

public class Assistents extends ListActivity{
	
	public ArrayList<Assistent> getAssistentsFromJSON(String JSON) {
		ArrayList<Assistent> assistents = new ArrayList<Assistent>();
		try {
			JSONObject bmaObject = new JSONObject(JSON);
			JSONArray jsonArray = bmaObject.getJSONArray("assistents");
			for(int i = 0; i < jsonArray.length(); i++ ){
        		JSONObject jsonObject = jsonArray.getJSONObject(i);
        		String nom = jsonObject.getString("nom");
        		String cognom = jsonObject.getString("cognom");
        		String empresa = jsonObject.getString("companyia");
        		String carrec = jsonObject.getString("carreg");
        		
        		URL newurl = null;
				try {
					newurl = new URL(jsonObject.getString("foto"));
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
        		
        		Bitmap foto = null;
        		try {
        			foto = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
        		}
        		catch (Exception e) {
        			System.out.println("merda");
        		}
        		assistents.add(new Assistent(nom, cognom, empresa, carrec, foto));
        	}
			return assistents;
		} 
		catch (JSONException e) {
			e.printStackTrace();
			return null;
			//jsonLabel.setText("error");
		}
	}
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.assistents_layout);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		if(Build.VERSION.SDK_INT > 11) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        	
		Intent intent = getIntent();
		String bmaFeed = intent.getStringExtra(Constants.EXTRA_ASSISTENTS);
		ArrayList<Assistent> assistents = getAssistentsFromJSON(bmaFeed);
		
		setListAdapter(new MyCustomAdapterView(this, assistents));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_download_manager, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
